package com.example.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.Dept;
import com.example.mapper.DeptMapper;
import com.example.service.IDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-03-26
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 获取部门组织结构
     * @return
     */
    @Override
    public ResultVo getDeptOrganization() {
        List<Dept> list = deptMapper.selectDeptCascade(-1);//-1为最上级部门编号
        return ResultVo.success("请求成功。",list);
    }
    /**
     * 添加部门
     * @param dept
     * @return
     */
    @Override
    @Transactional
    public ResultVo addDepartment(Dept dept) {
        Integer parentId = dept.getParentId();
        //1.获取同级部门下的排序
        Integer lastSort = getLastSort(parentId);
        dept.setSort(++lastSort);
        //2.插入数据
        int i = baseMapper.insert(dept);
        return i > 0 ? ResultVo.ok("添加成功。") : ResultVo.error("添加失败!");
    }

    @Override
    public PageInfo<Dept> getDeptPageInfo(String deptName, String address, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Dept> wrapper = new QueryWrapper<Dept>();
        wrapper.like(deptName != null, "dept_name", deptName)
                .like(address!=null,"address",address);
        List<Dept> deptList = baseMapper.selectList(wrapper);
        return new PageInfo<>(deptList);
    }

    @Override
    public ResultVo findChildren(Integer id) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Dept> deptList = baseMapper.selectList(wrapper);
        return ResultVo.success("请求成功",deptList);
    }

    @Override
    public ResultVo getDeptOptions(Integer companyId) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.select("id as value,dept_name as label")
                .eq(companyId!=null,"company_id",companyId);
        List<Map<String, Object>> maps = baseMapper.selectMaps(wrapper);

        maps.forEach(o->{
            o.forEach((a,b)->{
                System.err.println("a -> " + a);
                System.err.println("b -> " + b);
            });
        });

        return ResultVo.response(maps);
    }

    @Override
    public ResultVo getDeptList(Integer companyId) {
        List<Dept> deptList = baseMapper.selectDeptList(companyId);
        return ResultVo.response(deptList);
    }

    @Override
    public Boolean hasSomeDeptName(Integer id, String deptName, Integer companyId) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        wrapper.eq("dept_name",deptName)
                .eq("company_id",companyId)
                .notIn(id!=null,"id",id);
        List<Dept> deptList = baseMapper.selectList(wrapper);
        return deptList.size()!=0;
    }

    /**
     * 查询同级下最大排序
     * @param parentId
     * @return
     */
    public Integer getLastSort(Integer parentId){
        QueryWrapper<Dept> wrapper = new QueryWrapper<Dept>().select("max(sort) sort");
        Dept dept = baseMapper.selectOne(wrapper);
        return dept ==null ? 0 : dept.getSort();
    }
}
